package com.me.mysqladmin.controller;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.me.mysqladmin.entity.Location;
import com.me.mysqladmin.service.TableService;
import com.me.mysqladmin.util.EasyUIFormat;

@Controller
@RequestMapping("tableField")
public class TableFieldController {
	
	// 注入业务组件
	@Resource
	private TableService tableService;
	
	/**
	 * 跳转到主页
	 */
	@RequestMapping("/index.do")
	public String toIndex(){
		return "main/tableField";
	}
	
	/**
	 * 查询一张表的字段
	 */
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public Map<String, Object> queryAll(Location loc){
		if(loc.getDatabaseName() == null || "".equals(loc.getDatabaseName().trim())){
			return EasyUIFormat.parseGrid(Collections.emptyList(), LinkedHashMap.class);
		}
		if(loc.getTableName() == null || "".equals(loc.getTableName().trim())){
			return EasyUIFormat.parseGrid(Collections.emptyList(), LinkedHashMap.class);
		}
		List<Map<String, Object>> fields = tableService.showTableFields(loc);
		return EasyUIFormat.parseGrid(fields, LinkedHashMap.class);
	}
}
